package com.xpgk.mvc.productPrice;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.jfinal.aop.Before;
import com.jfinal.log.Log;
import com.xpgk.dto.RenderBean;
import com.xpgk.mvc.base.BaseController;
import com.xpgk.mvc.organization.Organization;
import com.xpgk.mvc.right.RightController;

/**
 * 产品价格设置
 * @author think
 *
 */
public class ProductPriceSetController extends BaseController {

	@SuppressWarnings("unused")
	private static final Log log = Log.getLog(RightController.class);
	
	private ProductPriceSetService productPriceSetService;
	
	public void index() {
		setAttr("curOrgan", Organization.dao.findById(getCUserOrganIds()));
		render("priceSet.html");
	}
	 
	/**
	 * 价格表下拉
	 */
	public void priceSelect(){
		String organIds = getPara("organIds", getCUserOrganIds());
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("column", Price.column_organizationIds);
		String sql = getSqlByBeetl(Price.sqlId_column1, param);
		List<Price> prices = Price.dao.find(sql,organIds);
		renderJson(prices);
	}
	
	
	
	
	
	/**
	 * 产品价格列表
	 */
	public void productPriceList(){
		Organization  o = Organization.dao.findById(getCUserOrganIds());
		if(o.getInt(Organization.column_level)==4){
			splitPage.getQueryParam().put("priceIds", o.getStr(Organization.column_priceIds));
		}
		
		String json = paging(splitPage, Price.sqlId_splitPageSelect, Price.sqlId_splitPageFrom);
		renderJson(json);
	}
	
	/**
	 * 跳转至添加
	 */
	public void toAddPrice(){
		Organization o = null;
		if (StringUtils.isNotBlank(getPara("organIds"))) {
			Price  p = Price.dao.findFirst("select *  from  sb_shop_price   where  organizationIds = ?  and dataStatus = 1",getPara("organIds"));
			o = Organization.dao.findById(getPara("organIds"));
			if (o == null) {
				o = new Organization();
				o.set(Organization.column_ids, getPara("organIds"));
			}
			if(p!=null){
				setAttr("price", p);
				setAttr("organ", o);
				render("upPrice.html");
			}else{
				setAttr("organ", o);
				render("addPrice.html");
			}
		}
		
	}
	
	
	
	public void cjPrice(){
		Price  p = Price.dao.findById(getPara("priceIds"));
		if(p.get(Price.column_organizationIds).equals(getPara("organIds"))){
			renderJson("1");
		}
	}
	
	/**
	 * 添加价格表
	 */
	@Before(ProductPriceValidator.class)
	public void addPrice(){
		List<ProductPrice> productPrices = getModels(ProductPrice.class);
		Price price = getModel(Price.class);
		RenderBean renderBean = productPriceSetService.addPrice(productPrices, price);
		renderJson(renderBean);
	}
	
	/**
	 * 跳转至修改
	 */
	public void toUpdatePrice(){
		setAttr("price", Price.dao.findById(getPara("priceIds")));
		setAttr("organ", Organization.dao.findById(getPara("organIds")));
		render("upPrice.html");
	}
	
	
	/**
	 * 修改价格表
	 */
	@Before(ProductPriceValidator.class)
	public void updatePrice(){
		List<ProductPrice> productPrices = getModels(ProductPrice.class);
		Price price = getModel(Price.class);
		RenderBean renderBean = productPriceSetService.updatePrice(productPrices, price);
		renderJson(renderBean);
	}
	
	/**
	 * 历史价格
	 */
	public void oldPriceList(){
		String json = paging(splitPage, ProductPrice.sqlId_oldPriceSplitPageSelect, ProductPrice.sqlId_oldPriceSplitPageFrom);
		renderJson(json);
	}
	
}
